﻿Public Class frmARSalesOrderEntry
    Dim dbSystem As New dbSystem
    Dim dbSales As New dbSales
    Dim Var As New varDeclare
    Dim frmCtrl As New FormControl
    Dim curr2word As New Currency
    Dim dsBase As DataSet
    Dim dsCustomer As DataSet
    Dim dsCurr As DataSet
    Dim dsItem As DataSet
    Dim dgvTemp As New DataGridView
    Dim TotalDebet As Decimal
    Dim TotalCredit As Decimal
    Public EntryID As String
    Dim ca As String
    Dim TypeAct As String

    Private Sub GenerateField()
        dsCurr = dbSystem.CurrList(Var.CompID)
        If (dsCurr.Tables(0).Rows.Count > 0) Then
            For i = 0 To dsCurr.Tables(0).Rows.Count - 1
                Try
                    cbDocCurr.Items.Add(dsCurr.Tables(0).Rows(i)("ID").ToString().Trim())
                Catch ex As Exception
                End Try
            Next
        End If

        '' ComboBox Salutaion
        Dim dtSalutation As DataTable = dbSystem.FreeQueryDataTable("SELECT TAB_TEXT ID FROM TABLE_VALUE WHERE TAB_TYPE = 'SALUTATION'")
        If (dtSalutation.Rows.Count > 0) Then
            For i = 0 To dtSalutation.Rows.Count - 1
                Try
                    cmbAttSolut.Items.Add(dtSalutation.Rows(i)("ID").ToString().Trim())
                Catch ex As Exception
                End Try
            Next
        End If

        '' ComboBox Incoterms
        Dim Incoterms As New List(Of comboItem)
        Incoterms.Add(New comboItem("Ex-Works ITALY", "0"))
        Incoterms.Add(New comboItem("FOB", "1"))
        Incoterms.Add(New comboItem("CIF", "2"))
        Incoterms.Add(New comboItem("Delivered", "3"))
        cmbIncoterms.DataSource = Incoterms
        cmbIncoterms.DisplayMember = "Text"
        cmbIncoterms.ValueMember = "Value"

        '' ComboBox Payment
        Dim PriceType As New List(Of comboItem)
        PriceType.Add(New comboItem("Delivered", "0"))
        PriceType.Add(New comboItem("Ex-Works", "1"))
        cmbTypePrice.DataSource = PriceType
        cmbTypePrice.DisplayMember = "Text"
        cmbTypePrice.ValueMember = "Value"
    End Sub

    Private Sub frmARSalesOrderEntry_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dsCustomer = New dbSystem().FreeQueryDataSet("SELECT [Code], [Company], [Term Day] FROM v_CustAccountList")
        advCustomer.Visible = False
        advCustomer.Initialize(dsCustomer, "Code", txtCustCode)
        GenerateField()
        dsItem = dbSales.GetItemList()
        cbDocCurr.Text = New dbSystem().FreeQueryString("SELECT TOP 1 CURR_BASE FROM CURRENCIES WITH(NOLOCK)")
        txtSONumber.Text = EntryID
        dgvTemp.Columns.Add("clID", "clID")
        dgvTemp.Rows.Add(EntryID)
        ToolbarHeader1.DefaultRow = 1
        ToolbarHeader1.Iniliaze("Entry : Quotation", Me, New frmQuotationList, dgvTemp, "clID")
        If (EntryID = "") Then
            ToolbarHeader1.ActionCtrl(False, False, False, False, False, False, False, False, True)
            txtSONumber.Text = dbSales.LastNumberSalesOrder(dtDocDate.Value.ToString("yyyy"))
            TypeAct = "I"
            txtCharges.Text = "0.00"
        Else
            TypeAct = "U"
            ToolbarHeader1.ActionCtrl(True, False, True, False, True, False, False, True, True)
            TabControl1.Enabled = False
            dgvBase.ReadOnly = True
            dgvBase.ContextMenuStrip = Nothing
            btnQuotationCode.Enabled = False
            Dim dtHeader As DataTable = New dbSystem().FreeQueryDataTable("SELECT * FROM v_SalesOrderList WHERE Number = '" & EntryID & "' AND COMP = '" & Var.CompID & "'")
            cbDocCurr.Text = dtHeader.Rows(0)("Curr").ToString().Trim()
            lblRev.Text = dtHeader.Rows(0)("REV").ToString().Trim()
            txtCustCode.Text = dtHeader.Rows(0)("Cust Code").ToString().Trim()
            'txtCustCompany.Text = dtHeader.Rows(0)("Customer").ToString().Trim()
            txtDeliveryPhone.Text = dtHeader.Rows(0)("SO_INVOICE_PHONE").ToString().Trim()
            txtDeliveryFax.Text = dtHeader.Rows(0)("SO_INVOICE_FAX").ToString().Trim()
            txtRef.Text = dtHeader.Rows(0)("PO / Reference").ToString().Trim()
            cmbAttSolut.Text = dtHeader.Rows(0)("SALUTATION").ToString().Trim()
            txtAttentionname.Text = dtHeader.Rows(0)("SO_ATTN").ToString().Trim()
            cmbIncoterms.Text = dtHeader.Rows(0)("INCOTERMS").ToString().Trim()
            txtEmail.Text = dtHeader.Rows(0)("SO_EMAIL").ToString().Trim()
            txtTerms.Text = dtHeader.Rows(0)("TERMS").ToString().Trim()
            dtRef.Value = CDate(dtHeader.Rows(0)("PO Date").ToString().Trim())
            dtDocDate.Value = CDate(dtHeader.Rows(0)("SO Date").ToString().Trim())
        End If
    End Sub

    Private Sub btnQuotationCode_Click(sender As Object, e As EventArgs) Handles btnQuotationCode.Click
        Dim formFind As New frmFind
        formFind.lblTitle.Text = "Search : GL Journal Group"
        formFind.QueryHeader = "SELECT Number [QuotationCode], CustomerPO, DateCustomerPO, Customer FROM v_QuotationList WHERE CustomerPO IS NOT NULL AND COMP = " + Var.CompID
        formFind.StartPosition = FormStartPosition.CenterScreen
        formFind.type = "lbl"
        formFind.lblValue = lblQuotationCode
        formFind.Show()
        MDI.Enabled = False
    End Sub

    Private Sub lblQuotationCode_TextChanged(sender As Object, e As EventArgs) Handles lblQuotationCode.TextChanged
        Dim dtQuotation As DataTable = dbSystem.FreeQueryDataTable("SELECT * FROM v_QuotationList WHERE Number = '" & lblQuotationCode.Text & "' AND COMP = '" & Var.CompID & "'")
        If (dtQuotation.Rows.Count > 0) Then
            txtCustCode.Text = dtQuotation.Rows(0)("Cust Code").ToString().Trim()
            cbDocCurr.Text = dtQuotation.Rows(0)("Curr").ToString().Trim()
            txtAttentionname.Text = dtQuotation.Rows(0)("Client").ToString().Trim()
            cmbAttSolut.Text = dtQuotation.Rows(0)("SALUTATION").ToString().Trim()
            txtDeliveryPhone.Text = dtQuotation.Rows(0)("PHONE").ToString().Trim()
            txtDeliveryFax.Text = dtQuotation.Rows(0)("FAX").ToString().Trim()
            txtEmail.Text = dtQuotation.Rows(0)("PRO_EMAIL").ToString().Trim()
            txtRef.Text = dtQuotation.Rows(0)("CustomerPO").ToString().Trim()
            dtRef.Value = CDate(dtQuotation.Rows(0)("DateCustomerPO").ToString().Trim())
            cmbIncoterms.Text = dtQuotation.Rows(0)("INCOTERMS").ToString().Trim()
            cmbTypePrice.Text = dtQuotation.Rows(0)("PRICE_TYPE").ToString().Trim()
            txtTerms.Text = dtQuotation.Rows(0)("TERMS").ToString().Trim()
            Dim dsLine As DataSet = dbSales.GetQuotationLine(lblQuotationCode.Text)
            If (dsLine.Tables(0).Rows.Count() > 0) Then
                ca = "LOAD"
                dgvBase.Rows.Add(dsBase.Tables(0).Rows.Count)
                For i = 0 To dsBase.Tables(0).Rows.Count - 1
                    Try
                        dgvBase.Rows(i).Cells("clChoose").Value = dsLine.Tables(0).Rows(i)("ITEM").ToString().Trim()
                        dgvBase.Rows(i).Cells("clDesc").Value = dsLine.Tables(0).Rows(i)("IC_DESCRIPTION").ToString().Trim()
                        dgvBase.Rows(i).Cells("clPriceList").Value = dsLine.Tables(0).Rows(i)("PRO_UNIT_PRICE").ToString().Trim()
                        dgvBase.Rows(i).Cells("clDisc").Value = dsLine.Tables(0).Rows(i)("PRO_DISC").ToString().Trim()
                        dgvBase.Rows(i).Cells("clNetPrice").Value = dsLine.Tables(0).Rows(i)("PRO_NET_PRICE").ToString().Trim()
                        dgvBase.Rows(i).Cells("clUOM").Value = dsLine.Tables(0).Rows(i)("PRO_UOM").ToString().Trim()
                        dgvBase.Rows(i).Cells("clQty").Value = dsLine.Tables(0).Rows(i)("PRO_QTY").ToString().Trim()
                        dgvBase.Rows(i).Cells("clNetAmount").Value = dsLine.Tables(0).Rows(i)("PRO_AMOUNT").ToString().Trim()
                        dgvBase.Rows(i).Cells("clDiscReal").Value = dsLine.Tables(0).Rows(i)("PRO_DISC").ToString().Trim()
                        dgvBase.Rows(i).Cells("clNetReal").Value = dsLine.Tables(0).Rows(i)("PRO_NET_PRICE").ToString().Trim()
                    Catch ex As Exception
                    End Try
                Next
                ca = "FINISH"
            End If
        End If
    End Sub

    Private Sub txtCustCode_TextChanged(sender As Object, e As EventArgs) Handles txtCustCode.TextChanged
        advCustomer.Visible = False
        If (ca = "FINISH") Then
            txtDeliveryPhone.Text = ""
            txtDeliveryFax.Text = ""
            txtAttentionname.Text = ""
            cmbAttSolut.Text = ""
            txtEmail.Text = ""
            If (txtCustCode.Text = "load") Then
                txtCustCode.Text = advCustomer.dsResult.Tables(0).Rows(0)("Code").ToString()
            ElseIf (txtCustCode.Text.Trim() <> "load") Then
                Dim dv As DataView = New DataView(dsCustomer.Tables(0))
                dv.RowFilter = "[Code] = '" & txtCustCode.Text.Trim() & "'"
                txtCustCompany.Text = dv.Item(0).Row("Company").ToString()
            Else
                txtCustCompany.Text = ""
            End If
        End If
    End Sub

    Private Sub txtN2_Leave(sender As Object, e As EventArgs) Handles txtCharges.Leave, txtDocRate.Leave
        Dim txt As TextBox = sender
        If (IsNumeric(txt.Text)) Then
            txt.Text = curr2word.dec2curr(txt.Text)
        End If
    End Sub

    Private Sub txtCurr_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtCharges.Validating
        Dim txt As TextBox = sender
        If (txt.Text.Trim() <> "") Then
            If (IsNumeric(txt.Text) = False) Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub txtCurr_Enter(sender As Object, e As EventArgs) Handles txtCharges.Enter, txtDocRate.Enter
        Dim txt As TextBox = sender
        If (IsNumeric(txt.Text)) Then
            txt.Text = Convert.ToDecimal(txt.Text).ToString()
            BeginInvoke(DirectCast(Sub() txt.SelectAll(), Action))
        End If
    End Sub

    Private Sub txtCurr_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCharges.KeyPress, txtDocRate.KeyPress
        Dim txt As TextBox = sender
        Dim FullStop As Char
        FullStop = "."
        If e.KeyChar = FullStop And txt.Text.IndexOf(FullStop) <> -1 Then
            e.Handled = True
            Return
        End If
        If Not Char.IsDigit(e.KeyChar) Then
            If (e.KeyChar <> FullStop) And
               (e.KeyChar <> Convert.ToChar(Keys.Back)) Then
                e.Handled = True
                Return
            End If
        End If
    End Sub

    Private Function toN0(txt As String) As String
        If (IsNumeric(txt) = False) Then txt = "0"
        Return Convert.ToDecimal(txt).ToString("N0", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
    End Function

    Private Function toN4(txt As String) As String
        If (IsNumeric(txt) = False) Then txt = "0"
        Return Convert.ToDecimal(txt).ToString("N4", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
    End Function

    Private Sub btnSearchAttention_Click(sender As Object, e As EventArgs) Handles btnSearchAttention.Click
        If (txtCustCompany.Text <> "") Then
            Dim formFind As New frmFind
            formFind.lblTitle.Text = "Search : GL Journal Group"
            formFind.QueryHeader = "SELECT (CUST_OFF_MNG + '-' + CAST(CCID_SEQ_NUMBER AS VARCHAR) [SEQ], SALUTATION [Salutation], CCID_CONTACT_NAME [Full Name], CCID_POSITION [Position], CCID_EMAIL [Email] FROM CUSTOMER_CONTACT WHERE CUSTOMER_CODE = '" & txtCustCode.Text & "' AND CCID_CONTACT_NAME IS NOT NULL"
            formFind.StartPosition = FormStartPosition.CenterScreen
            formFind.type = "txt"
            formFind.txtValue = txtSeq
            formFind.Show()
            MDI.Enabled = False
        End If
    End Sub

    Private Sub txtSeq_TextChanged(sender As Object, e As EventArgs) Handles txtSeq.TextChanged
        If (txtSeq.Text.Trim().Length > 0) Then
            Dim seq As Array = txtSeq.Text.Split("-")
            Dim dtContact As DataTable = New dbSystem().FreeQueryDataTable("SELECT TOP 1 SALUTATION, CCID_CONTACT_NAME, CCID_EMAIL FROM CUSTOMER_CONTACT WHERE CUSTOMER_CODE = '" & txtCustCode.Text & "' AND CUST_OFF_MNG = '" & seq(0) & "' AND CCID_SEQ_NUMBER = '" & seq(1) & "' AND CCID_CONTACT_NAME IS NOT NULL")
            If (dtContact.Rows.Count > 0) Then
                cmbAttSolut.Text = dtContact.Rows(0)("SALUTATION").ToString().Trim()
                txtAttentionname.Text = dtContact.Rows(0)("CCID_CONTACT_NAME").ToString().Trim()
                txtEmail.Text = dtContact.Rows(0)("CCID_EMAIL").ToString().Trim()
            Else
                cmbAttSolut.Text = ""
                txtAttentionname.Text = ""
                txtEmail.Text = ""
            End If
        Else
            txtAttentionname.Text = ""
            cmbAttSolut.Text = ""
            txtEmail.Text = ""
        End If
    End Sub

    Private Function validateForm() As Boolean
        Dim val As Boolean = True
        If (txtSONumber.Text.Trim() = "" Or txtCustCompany.Text.Trim() = "" Or cbDocCurr.Text = "" Or txtDocRate.Text.Trim() = "" Or _
            txtRef.Text.Trim() = "" Or cmbIncoterms.Text = "") Then
            val = False
        End If
        If (val = False) Then MessageBox.Show("Please Check All Field With Star Mark (*)")
        Return val
    End Function

    ''================================== Control Header ==============================================''
    Private Sub tsNew_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsNewClick
        Dim OpenForm As New frmARSalesOrderEntry
        OpenForm.EntryID = ""
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsEdit_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsEditClick
        TypeAct = "U"
        ToolbarHeader1.ActionCtrl(False, False, False, False, False, True, True, False, True)
        dgvBase.ReadOnly = False
        TabControl1.Enabled = True
        dtDocDate.Enabled = False
        dgvBase.ContextMenuStrip = cmsGL
        If (lblRev.Text = "-") Then
            lblRev.Text = "A"
        Else
            Dim ascii As Integer = Asc(lblRev.Text)
            lblRev.Text = Chr(ascii + 1)
        End If
    End Sub

    Private Sub tsSave_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsSaveClick
        If (validateForm()) Then
            Dim x As Array = txtSONumber.Text.Split("-")
            Dim VAT As String = "0"
            If (ckbVAT.Checked) Then VAT = "1"
            EntryID = dbSales.SaveSalesOrder(lblQuotationCode.Text, _
                                            x(0).ToString(), _
                                            dtDocDate.Value.ToString(Var.DateFormat), _
                                            txtCustCode.Text, _
                                            txtRef.Text, _
                                            dtRef.Value.ToString(Var.DateFormat), _
                                            txtInvoiceAddress.Text, _
                                            txtInvoiceCity.Text, _
                                            txtInvoiceZip.Text, _
                                            txtInvoiceCountry.Text, _
                                            txtInvoicePhone.Text, _
                                            txtInvoiceFax.Text, _
                                            txtDeliveryAddress.Text, _
                                            txtDeliveryCity.Text, _
                                            txtDeliveryZip.Text, _
                                            txtDeliveryCountry.Text, _
                                            txtDeliveryPhone.Text, _
                                            txtDeliveryFax.Text, _
                                            txtNotes.Text, _
                                            VAT, _
                                            cbDocCurr.Text, _
                                            lblSubTotalAmount.Text, _
                                            lblVAT.Text, _
                                            txtCharges.Text, _
                                            lblTotal.Text, _
                                            cmbAttSolut.Text, _
                                            txtAttentionname.Text, _
                                            txtEmail.Text, _
                                            cmbIncoterms.Text, _
                                            txtTerms.Text, _
                                            MDI.LoginUserName, _
                                            cmbTypePrice.Text, _
                                            lblRev.Text, _
                                            TypeAct)
            Dim Seq As Integer
            Dim status As Boolean = True
            If (EntryID <> "failed") Then
                For i = 0 To dgvBase.Rows.Count - 2
                    If (status) Then
                        If (dgvBase.Rows(i).Cells("clQty").Value.ToString() <> "") Then
                            Seq = i + 1
                            Dim GroupCode As Array = dgvBase.Rows(i).Cells("clChoose").Value.ToString().Split("-")
                            status = dbSales.SaveSalesOrderLine(txtCustCode.Text, _
                                                               dtDocDate.Value.ToString(Var.DateFormat), _
                                                               EntryID, _
                                                               GroupCode(0), _
                                                               GroupCode(1), _
                                                               dgvBase.Rows(i).Cells("clDesc").Value.ToString(), _
                                                               cbDocCurr.Text, _
                                                               dgvBase.Rows(i).Cells("clPriceList").Value.ToString(), _
                                                               dgvBase.Rows(i).Cells("clDisc").Value.ToString(), _
                                                               dgvBase.Rows(i).Cells("clNetPrice").Value.ToString(), _
                                                               dgvBase.Rows(i).Cells("clUOM").Value.ToString(), _
                                                               dgvBase.Rows(i).Cells("clQty").Value.ToString(), _
                                                               dgvBase.Rows(i).Cells("clNetAmount").Value.ToString(), _
                                                               dgvBase.Rows(i).Cells("clDeliveryDate").Value.ToString(Var.DateFormat), _
                                                               MDI.LoginUserName)

                        End If
                    End If
                Next
                If (status) Then
                    frmCtrl.openChild(frmARSalesOrderList, Me.ParentForm)
                Else
                    MessageBox.Show("Saving Sales Order " + EntryID + " , Failed at Seq" + Seq.ToString())
                End If
            Else
                MessageBox.Show("Saving Sales Order Failed")
            End If
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsPrintClick
        'Dim journalCode As Array = EntryID.Split("-")
        'Dim JournalNo As Array = journalCode(1).ToString().Split("/")
        'Dim dsReport As DataSet = New dbReport().GenerateJournalReport(journalCode(0).ToString().Trim(), JournalNo(1).ToString().Trim(), "", JournalNo(0).ToString().Trim(), JournalNo(0).ToString().Trim())
        'If (dsReport IsNot Nothing) Then
        '    Dim frmViewReport As New ViewReport
        '    frmViewReport.strReport = "Journal"
        '    frmViewReport.dsReport = dsReport
        '    frmViewReport.Show()
        'End If
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsRefreshClick
        Dim OpenForm As New frmARSalesOrderEntry
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsCancelClick
        Dim OpenForm As New frmARSalesOrderEntry
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsApprove_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsApproveClick
        'If (EntryID.Trim() <> "") Then
        '    gbApprove.Visible = True
        '    pnlBase.Enabled = False
        'Else : MessageBox.Show("Please Choose Quotation")
        'End If
    End Sub
    ''================================== Control Header [End] ==============================================''

    Private Sub ckbDeliveryAddress_CheckedChanged(sender As Object, e As EventArgs) Handles ckbDeliveryAddress.CheckedChanged
        If (ckbDeliveryAddress.Checked) Then
            pnlDelivery.Enabled = True
        Else
            pnlDelivery.Enabled = False
            txtDeliveryAddress.Text = txtInvoiceAddress.Text
            txtDeliveryCity.Text = txtInvoiceCity.Text
            txtDeliveryZip.Text = txtInvoiceZip.Text
            txtDeliveryCountry.Text = txtInvoiceCountry.Text
            txtDeliveryPhone.Text = txtInvoicePhone.Text
            txtDeliveryFax.Text = txtInvoiceFax.Text
        End If
    End Sub

    Private Sub btnInvoiceAddress_Click(sender As Object, e As EventArgs) Handles btnInvoiceAddress.Click
        If (txtCustCompany.Text <> "") Then
            Dim formFind As New frmFind
            formFind.lblTitle.Text = "Search : GL Journal Group"
            formFind.QueryHeader = "SELECT Office, Addr, Phone, Fax FROM vCustomer WHERE code = " + txtCustCode.Text
            formFind.StartPosition = FormStartPosition.CenterScreen
            formFind.type = "txt"
            formFind.txtValue = txtInvoiceOffice
            formFind.Show()
            MDI.Enabled = False
        End If
    End Sub

    Private Sub txtInvoiceOffice_TextChanged(sender As Object, e As EventArgs) Handles txtInvoiceOffice.TextChanged
        If (txtInvoiceOffice.Text.Trim().Length > 0) Then
            Dim dtPhone As DataTable = New dbSystem().FreeQueryDataTable("SELECT Addr, City, Zip, Country, Phone, Fax FROM vCustomer WHERE code = '" & txtCustCode.Text & "' AND office = '" & txtInvoiceOffice.Text & "'")
            If (dtPhone.Rows.Count > 0) Then
                txtInvoiceAddress.Text = dtPhone.Rows(0)("Addr").ToString().Trim()
                txtInvoiceCity.Text = dtPhone.Rows(0)("City").ToString().Trim()
                txtInvoiceZip.Text = dtPhone.Rows(0)("Zip").ToString().Trim()
                txtInvoiceCountry.Text = dtPhone.Rows(0)("Country").ToString().Trim()
                txtInvoicePhone.Text = dtPhone.Rows(0)("Phone").ToString().Trim()
                txtInvoiceFax.Text = dtPhone.Rows(0)("Fax").ToString().Trim()
            Else
                txtInvoiceAddress.Text = ""
                txtInvoiceCity.Text = ""
                txtInvoiceZip.Text = ""
                txtInvoiceCountry.Text = ""
                txtInvoicePhone.Text = ""
                txtInvoiceFax.Text = ""
            End If
        Else
            txtInvoiceAddress.Text = ""
            txtInvoiceCity.Text = ""
            txtInvoiceZip.Text = ""
            txtInvoiceCountry.Text = ""
            txtInvoicePhone.Text = ""
            txtInvoiceFax.Text = ""
        End If
    End Sub

    Private Sub btnDeliveryAddress_Click(sender As Object, e As EventArgs) Handles btnDeliveryAddress.Click
        If (txtCustCompany.Text <> "") Then
            Dim formFind As New frmFind
            formFind.lblTitle.Text = "Search : GL Journal Group"
            formFind.QueryHeader = "SELECT Office, Addr, Phone, Fax FROM vCustomer WHERE code = " + txtCustCode.Text
            formFind.StartPosition = FormStartPosition.CenterScreen
            formFind.type = "txt"
            formFind.txtValue = txtDeliveryOffice
            formFind.Show()
            MDI.Enabled = False
        End If
    End Sub

    Private Sub txtDeliveryOffice_TextChanged(sender As Object, e As EventArgs) Handles txtDeliveryOffice.TextChanged
        If (txtDeliveryOffice.Text.Trim().Length > 0) Then
            Dim dtPhone As DataTable = New dbSystem().FreeQueryDataTable("SELECT Addr, City, Zip, Country, Phone, Fax FROM vCustomer WHERE code = '" & txtCustCode.Text & "' AND office = '" & txtDeliveryOffice.Text & "'")
            If (dtPhone.Rows.Count > 0) Then
                txtDeliveryAddress.Text = dtPhone.Rows(0)("Addr").ToString().Trim()
                txtDeliveryCity.Text = dtPhone.Rows(0)("City").ToString().Trim()
                txtDeliveryZip.Text = dtPhone.Rows(0)("Zip").ToString().Trim()
                txtDeliveryCountry.Text = dtPhone.Rows(0)("Country").ToString().Trim()
                txtDeliveryPhone.Text = dtPhone.Rows(0)("Phone").ToString().Trim()
                txtDeliveryFax.Text = dtPhone.Rows(0)("Fax").ToString().Trim()
            Else
                txtDeliveryAddress.Text = ""
                txtDeliveryCity.Text = ""
                txtDeliveryZip.Text = ""
                txtDeliveryCountry.Text = ""
                txtDeliveryPhone.Text = ""
                txtDeliveryFax.Text = ""
            End If
        Else
            txtDeliveryAddress.Text = ""
            txtDeliveryCity.Text = ""
            txtDeliveryZip.Text = ""
            txtDeliveryCountry.Text = ""
            txtDeliveryPhone.Text = ""
            txtDeliveryFax.Text = ""
        End If
    End Sub

    Private Sub dtDocDate_ValueChanged(sender As Object, e As EventArgs) Handles dtDocDate.ValueChanged
        If (TypeAct <> "U") Then txtSONumber.Text = dbSales.LastNumberSalesOrder(dtDocDate.Value.ToString("yyyy"))
    End Sub
End Class